package com.saul.server.business.pc28.service.impl;

import com.saul.server.business.pc28.entity.ActualResult;
import com.saul.server.business.pc28.service.Canada28DataService;
import com.saul.server.business.utils.CulResultUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.ParseException;

/**
 * @author 枫伦
 * @DESCRIPTION 爬取固定网站上的固定信息
 * @create 2021/6/13 10:16 上午
 */
@Service
public class Canada28DataServiceImpl implements Canada28DataService {
    public static String url = "https://www.dashen28.com/jianada28/lishi/";

    @Override
    public ActualResult getActuakDataNewest() throws IOException, ParseException {
        Document document = Jsoup.connect(url).get();
        Element element = document.body().getElementsByTag("table").select("tbody").select("tr").get(0);
        Elements td = element.getElementsByTag("td");
        //期数
        String issueNumber = td.get(0).text().replaceAll(" ", "");
        //时间
//        String time = td.get(1).text();
//        int year = LocalDateTime.now().getYear();
//        SimpleDateFormat simpleDateFf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date parse = simpleDateFf.parse(year + "-" + time);
        //结果
        String element3 = td.get(2).text();
        String[] split = element3.split("\\+");
        String actualNum1 = split[0].replaceAll(" ", "");
        String actualNum2 = split[1].replaceAll(" ", "");
        String actualNum3 = split[2].split("=")[0].replaceAll(" ", "");
        String actualSum = split[2].split("=")[1].replaceAll(" ", "");
        String actualSize = CulResultUtil.getSize(actualSum);
        String actualSingleDouble = CulResultUtil.getSingleDouble(actualSum);
        ActualResult actualResult = new ActualResult();
        actualResult.setIssueNumber(Long.valueOf(issueNumber));
        actualResult.setActualNum1(Integer.valueOf(actualNum1));
        actualResult.setActualNum2(Integer.valueOf(actualNum2));
        actualResult.setActualNum3(Integer.valueOf(actualNum3));
        actualResult.setActualSum(Integer.valueOf(actualSum));
        actualResult.setActualSize(actualSize);
        actualResult.setActualSingleDouble(actualSingleDouble);
        return actualResult;
    }
}
